package cn.lang.oss.factory;

import cn.lang.oss.Oss;
import cn.lang.oss.OssKit;
import cn.lang.oss.UpyunOss;

import java.util.Properties;

/**
 * @ClassName : UpyunOssFactory
 * @Description : 又拍云工厂
 * @Author : Lang
 * @Date: 2020-03-07 08:50
 */
public class UpyunOssFactory implements IOssFactory{

    /**
     * 服务器名
     */
    private String bucket;

    /**
     * 操作员名
     */
    private String username;

    /**
     * 操作员密码
     */
    private String password;

    /**
     * 外链域名
     */
    private String domain;


    private static UpyunOss upyunOss;

    public UpyunOssFactory(){
        init();
    }

    public UpyunOssFactory(String username,String password,String bucket,String domain){
        this.username = username;
        this.password = password;
        this.bucket = bucket;
        this.domain = domain;
    }

    void init(){
        Properties resouce = OssKit.getResources();
        if(resouce==null){
            throw new RuntimeException("不能获取到配置文件oss.properties");
        }
        this.username = resouce.getProperty("oss.upy.username");
        this.password = resouce.getProperty("oss.upy.password");
        this.bucket = resouce.getProperty("oss.upy.bucket");
        this.domain = resouce.getProperty("oss.upy.domain");
    }

    public Oss getOss() {
        if(upyunOss!=null){
            return upyunOss;
        }
        init();
        UpyunOss upyunOss = new UpyunOss(this.username,this.password,this.bucket,this.domain);
        UpyunOssFactory.upyunOss = upyunOss;
        return upyunOss;
    }


    public Oss getOss(String username,String password,String bucket,String domain) {
        UpyunOss upyunOss = new UpyunOss(username,password,bucket,domain);
        UpyunOssFactory.upyunOss = upyunOss;
        return upyunOss;
    }
}
